const query = require('./fun')
const sqlCount = 'SQL_CALC_FOUND_ROWS';
const { ADMIN, USER, PERFORMER, PERFORMERTYPE, MOVIE, MOVIETAG, COMMENT, SENSITIVES, MEDIA, NEWS } = require('./database')
let { sqlUpdateStr, sqlAddStr, sqlSelectStr, sqlWhereStr } = require('../util.js');
/**
 * 所有请求方法
 */
/**
 *  登录
 */
function isAdminLogin(data) {
    return new Promise((resolve, reject) => {
        query(`select  * from ${ADMIN} where name=? and password=?`, data)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取用户的数目
 */
function getUserCount() {
    return new Promise((resolve, reject) => {
        query(`select count(userId) as counts from ${USER}`)
            .then((res) => {
                resolve(res[0]['counts'])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取分页用户数
 */
function getUserList(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        Object.entries(like).forEach((item) => {
            if (item[1] != undefined && item[1] != "") {
                if (!String(condition).length) {
                    condition += " where "
                } else {
                    condition += " and "
                }
                condition += `${item[0]} like '%${item[1]}%'`
            }
        })
        query(`select  ${sqlCount} * from ${USER} ${condition}  limit ?,${limit}`, { num }, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取管理员总数目
 */
function getAdminCount() {
    return new Promise((resolve, reject) => {
        query(`select count(id) as counts from ${ADMIN}`)
            .then((res) => {
                resolve(res[0]['counts'])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取管理员全部信息
 */
function GetAdminList(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        query(`select ${sqlCount} * from ${ADMIN} ${condition}  limit ?,${limit}`, { num }, true)
            .then(async (res) => {
                resolve(res);
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据管理员id查询管理员信息
 */
function GetAdminById(id) {
    return new Promise((resolve, reject) => {
        query(`select * from ${ADMIN} where id=?`, { id })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据id修改管理员信息
 */
function UpdateAdminById(id, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${ADMIN} SET ${sql} WHERE id=${id}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据id删除管理员
 */
function DeleteAdminById(id) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${ADMIN} WHERE id=?`, id)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 添加管理员
 */
function addAdmin(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${ADMIN}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据id获取用户信息
 */
function GetUsersById(id) {
    return new Promise((resolve, reject) => {
        query(`select * from ${USER} where userId=?`, { id })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 删除用户的方法
 */
function DeleteUserById(id) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${USER} WHERE userId=?`, { id })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获得影人总数
 */
function getPerformerCount() {
    return new Promise((resolve, reject) => {
        query(`select count(performerId) as counts from ${PERFORMER}`)
            .then((res) => {
                resolve(res[0]['counts'])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取分页影人数
 */
function getPerformerList(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        query(`select  ${sqlCount} * from ${PERFORMER} ${condition}  limit ?,${limit}`, { num }, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 影人的类型
 */
function GetperformerType() {
    return new Promise((resolve, reject) => {
        query(`select *  from ${PERFORMERTYPE}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 添加影人
 */
function addPerformer(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${PERFORMER}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 查看影人
 */
function getPerformerById(id) {
    return new Promise((resolve, reject) => {
        query(`select * from ${PERFORMER} where performerId=?`, { id })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改影人信息
 */
function updatePerformerById(id, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${PERFORMER} SET ${sql} WHERE performerId=${id}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 删除影人
 */
function DeleteperformerById(performerId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${PERFORMER} WHERE performerId=?`, { performerId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取电影数目
 */
function getMovieCount() {
    return new Promise((resolve, reject) => {
        query(`select count(movieId) as counts from ${MOVIE}`)
            .then((res) => {
                resolve(res[0]['counts'])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取电影信息列表
 */
function getMovieList(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        query(`select  ${sqlCount} * from ${MOVIE} ${condition}  limit ?,${limit}`, { num }, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取电影的信息
 */
function GetmovieById(movieId) {
    return new Promise((resolve, reject) => {
        query(`select * from ${MOVIE} where movieId=?`, { movieId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 删除电影
 */
function DeletemovieById(movieId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${MOVIE} WHERE movieId=?`, { movieId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取电影标签
 */
function Getmovietag() {
    return new Promise((resolve, reject) => {
        query(`select *  from ${MOVIETAG}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 添加电影
 */
function addmovie(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${MOVIE}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改电影信息
 */
function UpdateMovie(movieId, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${MOVIE} SET ${sql} WHERE movieId=${movieId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 电影标签全部数目
 */
function getmovietagCount() {
    return new Promise((resolve, reject) => {
        query(`select count(movietagId) as counts from ${MOVIETAG}`)
            .then((res) => {
                resolve(res[0]['counts'])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取电影标签全部信息
 */
function getmovietagList(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        query(`select  ${sqlCount} * from ${MOVIETAG} ${condition}  limit ?,${limit}`, { num }, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 添加标签
 */
function addmovietag(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${MOVIETAG}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}/**
 * 
 *删除电影标签
 */
function DeletemovietagById(movietagId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${MOVIETAG} WHERE movietagId=?`, { movietagId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取电影标签信息
 */
function GetmovietagById(movietagId) {
    return new Promise((resolve, reject) => {
        query(`select * from ${MOVIETAG} where movietagId=?`, { movietagId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改电影标签信息
 */
function UpdatemovietagById(movietagId, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${MOVIETAG} SET ${sql} WHERE movietagId=${movietagId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取用户评论数目
 */
function getCommentCount() {
    return new Promise((resolve, reject) => {
        query(`select count(commentId) as counts from ${COMMENT}`)
            .then((res) => {
                resolve(res[0]['counts'])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取用户评论
 */
function getCommentList(num, limit, like = {}) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        if (!String(condition).length) {
            condition += ` where `
        } else {
            condition += ` and `
        }
        condition += `${USER}.userId=${COMMENT}.userId and ${MOVIE}.movieId=${COMMENT}.movieId`
        query(`select  ${sqlCount} ${COMMENT}.*,${USER}.userName,${MOVIE}.movieName from ${COMMENT},${USER},${MOVIE} ${condition}  limit ?,${limit}`, { num }, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 删除用户评论
 */
function DeletecommentById(commentId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${COMMENT} WHERE commentId=?`, { commentId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取所有用户
 */
function GetuserType() {
    return new Promise((resolve, reject) => {
        query(`select ${USER}.userName,${USER}.userId  from ${USER}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 添加评论
 */
function addcomment(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${COMMENT}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据id查看信息
 */
function GetcommentById(commentId) {
    return new Promise((resolve, reject) => {
        query(`select ${COMMENT}.*,${USER}.userName,${MOVIE}.movieName from ${COMMENT},${USER},${MOVIE} where commentId=? and ${COMMENT}.userId=${USER}.userId and ${MOVIE}.movieId=${COMMENT}.movieId`, { commentId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取电影选择类别
 */
function GetmovieType() {
    return new Promise((resolve, reject) => {
        query(`select ${MOVIE}.movieName,${MOVIE}.movieId  from ${MOVIE}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改评论
 */
function updatecomment(commentId, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${COMMENT} SET ${sql} WHERE commentId=${commentId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取所有敏感词
 */
function getSensitiveAll() {
    return new Promise((resolve, reject) => {
        query(`select *  from ${SENSITIVES}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 新增敏感词
 */
function addSensitive(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT ignore INTO ${SENSITIVES}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 删除敏感信息
 */
function delSensitive(sensitiveId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${SENSITIVES} WHERE sensitiveId=?`, { sensitiveId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取所有的评论
 */
function getCommentListAll(like = {}) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        if (!String(condition).length) {
            condition += ` where `
        } else {
            condition += ` and `
        }
        condition += `${USER}.userId=${COMMENT}.userId `
        query(`select  ${sqlCount} ${COMMENT}.*,${USER}.userName from ${COMMENT},${USER} ${condition}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取一周用户数据
 */
function getUserListWord() {
    return new Promise((resolve, reject) => {
        query(`select  ${sqlCount} * from ${USER} where YEARWEEK(date_format(userTime,'%Y-%m-%d'),1) = YEARWEEK(now(),1)`, {}, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    });
}
/**
 * 获取电影分类信息
 */
function getMovietypeTable() {
    return new Promise((resolve, reject) => {
        query([`select count(*) as hotNum from ${MOVIE} where movieType='热播'`, `select count(*)  as soonNum from ${MOVIE}  where movieType='即将上映'`])
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取影人分类
 */
function getPerformerTable() {
    return new Promise((resolve, reject) => {
        query([`select count(*) as performerType from ${PERFORMER} where performerType='演员'`, `select count(*)  as directorNum from ${PERFORMER}  where performerType='导演'`])
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取电影影人
 */
function Getperformer() {
    return new Promise((resolve, reject) => {
        query(`select *  from ${PERFORMER}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 *获取媒体的数目
 */
function getMediaCount() {
    return new Promise((resolve, reject) => {
        query(`select count(mediaId) as counts from ${MEDIA}`)
            .then((res) => {
                resolve(res[0]['counts'])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取媒体信息
 */
function getmediaList(num, limit, like = {}) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        query(`select  ${sqlCount} * from ${MEDIA} ${condition}  limit ?,${limit}`, { num }, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取所有媒体信息
 */
function getmediaitiveAll() {
    return new Promise((resolve, reject) => {
        query(`select *  from ${MEDIA}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 添加媒体信息
 */
function addMedia(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${MEDIA}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 删除媒体信息
 */
function deleteMediaById(mediaId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${MEDIA} WHERE mediaId=?`, { mediaId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取指定媒体的信息
 */
function getMediaById(mediaId) {
    return new Promise((resolve, reject) => {
        query(`select * from ${MEDIA} where mediaId=?`, { mediaId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改媒体信息
 */
function updateMedia(mediaId, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${MEDIA} SET ${sql} WHERE mediaId=${mediaId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取新闻列表
 */
function getnewsList(num, limit, like = {}) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        if (!String(condition).length) {
            condition += ` where `
        } else {
            condition += ` and `
        }
        condition += `${NEWS}.mediaId=${MEDIA}.mediaId`
        query(`select  ${sqlCount} ${NEWS}.*,${MEDIA}.* from ${NEWS},${MEDIA} ${condition}  limit ?,${limit}`, { num }, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 添加新闻
 */
function addNews(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${NEWS}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取新闻详情
 */
function GetnewsById(newsId) {
    return new Promise((resolve, reject) => {
        query(`select * from ${NEWS} where newsId=?`, { newsId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 删除新闻
 */
function DeletenewsById(newsId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${NEWS} WHERE newsId=?`, { newsId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改新闻
 */
function updataNews(newsId, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${NEWS} SET ${sql} WHERE newsId=${newsId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
exports.getPerformerCount = getPerformerCount;
exports.GetperformerType = GetperformerType;
exports.getPerformerList = getPerformerList;
exports.addPerformer = addPerformer;
exports.getPerformerById = getPerformerById;
exports.updatePerformerById = updatePerformerById;
exports.DeleteperformerById = DeleteperformerById;
exports.getMovieCount = getMovieCount;
exports.getMovieList = getMovieList;
exports.GetmovieById = GetmovieById;
exports.DeletemovieById = DeletemovieById;
exports.Getmovietag = Getmovietag;
exports.addmovie = addmovie;
exports.UpdateMovie = UpdateMovie;
exports.getmovietagCount = getmovietagCount;
exports.getmovietagList = getmovietagList;
exports.addmovietag = addmovietag;
exports.DeletemovietagById = DeletemovietagById;
exports.GetmovietagById = GetmovietagById;
exports.UpdatemovietagById = UpdatemovietagById;
exports.getCommentCount = getCommentCount;
exports.getCommentList = getCommentList;
exports.DeletecommentById = DeletecommentById;
exports.GetuserType = GetuserType;
exports.addcomment = addcomment;
exports.GetcommentById = GetcommentById;
exports.updatecomment = updatecomment;
exports.getSensitiveAll = getSensitiveAll;
exports.addSensitive = addSensitive;
exports.delSensitive = delSensitive;
exports.getCommentListAll = getCommentListAll;
exports.isAdminLogin = isAdminLogin;
exports.getUserCount = getUserCount;
exports.getUserList = getUserList;
exports.GetUsersById = GetUsersById;
exports.DeleteUserById = DeleteUserById;
exports.getAdminCount = getAdminCount;
exports.GetAdminById = GetAdminById;
exports.UpdateAdminById = UpdateAdminById;
exports.DeleteAdminById = DeleteAdminById;
exports.addAdmin = addAdmin;
exports.GetAdminList = GetAdminList;
exports.getUserListWord = getUserListWord;
exports.getMovietypeTable = getMovietypeTable;
exports.getPerformerTable = getPerformerTable;
exports.GetmovieType = GetmovieType;
exports.Getperformer = Getperformer;
exports.getMediaCount = getMediaCount;
exports.getmediaList = getmediaList;
exports.getmediaitiveAll = getmediaitiveAll;
exports.addMedia = addMedia;
exports.deleteMediaById = deleteMediaById;
exports.getMediaById = getMediaById;
exports.updateMedia = updateMedia;
exports.getnewsList = getnewsList;
exports.addNews = addNews;
exports.GetnewsById = GetnewsById;
exports.DeletenewsById=DeletenewsById;
exports.updataNews=updataNews;